from django.core.urlresolvers import get_callable

def to_python_integer_list(value):
    if not isinstance(value,basestring):
        raise ValueError,"Value must be a string"
    return tuple(map(int,value.split(',')))

def to_string_integer_list(values):
    output = []
    try:
        for value in values:
            output.append(str(int(value)))
    except TypeError:
        raise ValueError,"Values must be an iteratable"
    return ", ".join(output)

def to_python_callable(value):
    if not isinstance(value,basestring):
        raise ValueError,"Value must be a string"
    return get_callable(value)

def to_string_callable(value):
    if not callable(value):
        raise ValueError,"Value is not callable"
    if not hasattr(value,'__module__') or not hasattr(value,'__name__'):
        raise ValueError,"Value must be accessable via a module"
    return "{0}.{1}".format(value.__module__,value.__name__)

def to_python_callables(values):
    if isinstance(values,basestring):
        values = values.split(',')
    return tuple(map(lambda x: get_callable(str(x).strip()),values))

def to_string_callables(values):
    def to_string(value):
        if callable(value) and hasattr(value,'__module__') and hasattr(value,'__name__'):
            return "{0}.{1}".format(value.__module__,value.__name__)
        raise ValueError,"must be a callable accessable via a module"

    return tuple(map(to_string,values))

